/*
 * Created on Jul 11, 2005 TODO To change the template for this generated file
 * go to Window - Preferences - Java - Code Style - Code Templates
 */
package com.novix.utility.web;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.novix.portal.framework.exceptions.BusinessException;
import com.novix.portal.framework.util.IConstants;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.Session;
import lotus.domino.View;
import lotus.domino.cso.DocumentCollection;

/**
 * @author Dev TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class DominoLookup {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(DominoLookup.class);

    /**
     * 
     */
    private DominoLookup() {
        super();

    }
    public static Set getRegionSearchFields(String fieldName, String fieldType, String serverConnectionString, String token, String captive, String serverName, String lookupPath) {
        if (StringUtils.isEmpty(captive))
            captive = IConstants.CAPTIVE_GENERIC;
        Session session = null;        
        try {
            session = NotesFactory.createSession(serverConnectionString, Messages.getString("CacheLoader.name"), Messages.getString("CacheLoader.password"));
            Database notesDatabase = session.getDatabase(serverName, lookupPath);
            if (!notesDatabase.isOpen())
                notesDatabase.open();
            View view = notesDatabase.getView("(admin)");
            lotus.domino.DocumentCollection dc = view.getAllDocumentsByKey(captive);
            Document doc = dc.getFirstDocument();
            Document temp = null; //sets the temp for garbage collection
            // immediately
            String sValue = "";
            while (doc != null) {
                if (doc.getItemValueString("designElementName").compareToIgnoreCase(fieldType.trim()) == 0)
                    sValue = doc.getItemValueString("captiveurl");
                temp = dc.getNextDocument(doc); // get the next one
                doc.recycle(); // recycle the one we're done with
                doc = temp;
            }
            notesDatabase.recycle();
            view.recycle();
            dc.recycle();
            if(StringUtils.isEmpty(sValue))
            	return new TreeSet();
            String[] vals = sValue.split("~");
            Set set = new TreeSet();
            for (int i = 0; i < vals.length; i++) {
				set.add(vals[i]);
			}
            return set;
        } catch (Exception e) {
            logger.error(e);
            throw new BusinessException("Failure in domino lookup", e);
        } finally {
            try {
                if (session != null)
                    session.recycle();
            } catch (NotesException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    public static String getServiceProvidersURL(String fieldName, String fieldType, String serverConnectionString, String token, String captive, String serverName, String lookupPath) {
        if (StringUtils.isEmpty(captive))
            captive = IConstants.CAPTIVE_GENERIC;
        Session session = null;        
        try {
            session = NotesFactory.createSession(serverConnectionString, Messages.getString("CacheLoader.name"), Messages.getString("CacheLoader.password"));
            Database notesDatabase = session.getDatabase(serverName, lookupPath);
            if (!notesDatabase.isOpen())
                notesDatabase.open();
            View view = notesDatabase.getView("(admin)");
            lotus.domino.DocumentCollection dc = view.getAllDocumentsByKey(captive);
            Document doc = dc.getFirstDocument();
            Document temp = null; //sets the temp for garbage collection
            // immediately
            String sValue = null;
            while (doc != null) {
                if (doc.getItemValueString("designElementName").compareToIgnoreCase(fieldType.trim()) == 0)
                    sValue = doc.getItemValueString("captiveurl");
                temp = dc.getNextDocument(doc); // get the next one
                doc.recycle(); // recycle the one we're done with
                doc = temp;
            }
            notesDatabase.recycle();
            view.recycle();
            dc.recycle();
            return sValue;
        } catch (Exception e) {
            logger.error(e);
            throw new BusinessException("Failure in domino lookup", e);
        } finally {
            try {
                if (session != null)
                    session.recycle();
            } catch (NotesException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }

}
